Enhance get_items to handle large requests and expose partial errors - #155
Merged
Conversation
Requests for several items now keep the order they were asked for, are split into as many API calls as the item limit requires, and report the partial errors that Amazon returns for the items it could not resolve. - Split get_items into chunks and ask for duplicated identifiers once - Return the items in the order of the request, with include_unavailable to get a placeholder for the ones missing from the response - Expose the partial errors of a response in the returned list and in the message of ItemsNotFoundError - Raise InvalidArgumentError instead of leaking pydantic validation errors - Apply the timeout to the OAuth2 token refresh of the sync client, which waited indefinitely, and report its failures as AuthenticationError - Fix the test fixtures that used the casing of the old PA-API responses Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD
Failures were reported with the error codes of the old Product Advertising API, so anything that was not a 404 or a 429 arrived as a generic request error, and a throttled or failed request was never tried again even though the API documents that clients should back off and retry. - Map the errors from the response of the API, keeping the reason and the fields that failed in the message of the exception - Raise AuthenticationError for missing or expired credentials and the new AccessDeniedError for the ones without access to the operation - Add ResourceNotFoundError for feeds and reports, told apart from items - Retry throttled and server errors with the new retries parameter, waiting longer before every attempt and honouring the Retry-After header - Refresh an expired token once and send the request again - Wrap connection failures and unparseable responses in RequestError Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD
The async client built its requests by hand, so it accepted values that the API rejects and could drift away from the synchronous one. It now builds them with the models of the SDK, which also brings the parameters that were missing to both clients. - Add the availability parameter to search_items, and reject a search without any criteria instead of sending it to the API - Add host and auth_endpoint to both clients, and give every client its own configuration instead of the one shared by the whole process - Report the identifier Amazon gives to a request in the error message - Measure throttling with a monotonic clock, guarded by a lock so the synchronous client can be shared by several threads - Ship the py.typed marker and export get_asin and errors from the package - Fix the examples of the documentation that used names that do not exist, and the documented limits that did not match the ones of the API - Drop the unused six dependency Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR enhances the
get_itemsmethod to automatically handle requests larger than the API limit by splitting them into multiple calls, while maintaining the requested order and exposing partial errors from Amazon's responses.Key Changes
get_itemsnow splits requests with more than 10 items into multiple API calls, allowing any amount of items to be requested at onceResultListcontainer that extendslistto expose partial errors from Amazon responses via anerrorsattributeinclude_unavailableparameter adds placeholder items (with only ASIN) for requested items missing from the responseTimeoutOAuth2TokenManagerthat respects the configured timeout for authentication requests, preventing indefinite hangsbuild_requestutility that validates request parameters and raisesInvalidArgumentErrorfor invalid valuesImplementation Details
New utility modules:
amazon_creatorsapi/core/items.py: Functions for deduplication, chunking, and sorting itemsamazon_creatorsapi/core/results.py:ResultListcontainer for results with partial errorsamazon_creatorsapi/core/auth.py:TimeoutOAuth2TokenManagerfor timeout-aware token requestsamazon_creatorsapi/core/validation.py: Enhanced withbuild_requestfunctionUpdated
get_itemssignature to returnResultList[Item]instead oflist[Item]Both sync (
AmazonCreatorsApi) and async (AsyncAmazonCreatorsApi) implementations updated consistentlyComprehensive test coverage added for new functionality
Documentation updated with examples of the new capabilities
Breaking Changes
get_itemsnow returnsResultList[Item]instead oflist[Item](backward compatible asResultListextendslist)https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD